package ShareYourWay.webpages;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;


import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.markup.html.form.select.Select;
import org.apache.wicket.extensions.markup.html.form.select.SelectOption;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;







import ShareYourWay.functions.App;
import ShareYourWay.functions.Functions_Meetup;
import ShareYourWay.functions.MeetSearch;
import ShareYourWay.functions.SignInSession;
import ShareYourWay.project.Meetup;
import ShareYourWay.project.User;
import ShareYourWay.util.AuthPage;


@AuthorizeInstantiation("USER")
public class Meets  extends WebPage {
	
	//-------------- Nuevo ------------------------
	SignInSession s = SignInSession.get();
	public User user = s.getUser();
	
	
	private static final long serialVersionUID = 1L;
	
	//Random idmeet = new Random(); // Random provisional para generar el id del Meet
	private String selectedOrigin = "All";
	private String selectedDestination = "All";
	private String selectedVehicle = "Coche";
	private MeetSearch m;
	private Date fecha;
	private Date time;
	private static final List<String> Vehicle = Arrays.asList(new String[] {
			"Coche", "Moto", "Tren" });

	public Meets() {
		
		final Meetup meet = new Meetup();
		final Functions_Meetup fm = new Functions_Meetup();
		final App app = new App();
		
		
		m = new MeetSearch(selectedOrigin, selectedDestination);
		
		final Component MeetPanel;
		MeetPanel=new PanelMeets("PanelMeets",m);
		MeetPanel.setOutputMarkupId(true); // Nos permite cargar el Panel con el boton AJAX
	
		add(MeetPanel);
		
			
		Form<Meetup> FormNewMeet = new Form<Meetup>("form-newmeet");
		Form<Meetup> Sform = new Form<Meetup>("searchform");
		
		
		TextField<String> torigin = new TextField<String>("torigin", new PropertyModel<String>(meet, "origin"));
		torigin.setRequired(true);
		
		TextField<String> tdestination = new TextField<String>("tdestination", new PropertyModel<String>(meet, "destination"));
		tdestination.setRequired(true);	
		
		
		Select tslots = new Select("tslots", new PropertyModel<String>(
				meet, "slots"));	

		
		ListChoice<String> listOrigin = new ListChoice<String>("origin",
				new PropertyModel<String>(this, "selectedOrigin"), app.listOrigin());
		
		ListChoice<String> listDestination= new ListChoice<String>("destination",
				new PropertyModel<String>(this, "selectedDestination"), app.listdestination());
		
		ListChoice<String> listVehicle = new ListChoice<String>("tvehicle",
				new PropertyModel<String>(this, "selectedVehicle"), Vehicle);


        AjaxButton ButtonsaveMeet = new AjaxButton("savemeet") {

            /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                // TODO Auto-generated method stub
            	
            	// TODO Auto-generated method stub
				
				String vehicle = selectedVehicle;
				meet.setVehicle(vehicle);
				
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
				String date1 = format1.format(fecha); 

				@SuppressWarnings("deprecation")
				String hora =""+fecha.getHours()+":"+fecha.getMinutes();
				
				System.out.println("Fecha: "+date1+" Hora:"+hora);
				
				meet.setTimeMeet(hora);
				meet.setMdate(date1);
				Meetup m1 = new Meetup (meet); // Hago una copia del objeto, si no lo hago se sobreescribirá.
				
				

				

				
				app.addMeetup(meet);
				

				


				target.add(MeetPanel);
				target.appendJavaScript("cerrar_modal(); alertas('Evento creado');");
				setResponsePage(Meets.class);
				

            }

        };
        
		add( new AjaxLink( "signout" ){ 
			   public void onClick( AjaxRequestTarget target ){ 	  
		

				   
				 s.invalidate(); 					 
                 setResponsePage(HomePage.class); 
				   



				   
			   } 
			} ); 
		
		
        AjaxButton ButtonsearchMeet = new AjaxButton("search") {

            /**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                // TODO Auto-generated method stub
            	
            	// TODO Auto-generated method stub
				 
				m.setOrigin(selectedOrigin);
				m.setDestination(selectedDestination);
				
				System.out.println("-----Search: "+m.getOrigin()+" d:"+m.getDestination());

				
				MeetPanel.configure();

				target.add(MeetPanel);
				


            }

        };
		
		
 //---------------------- Profile -------------------------
		
				Label name = new Label ("pname", new PropertyModel<String>(user, "name"));
				Label surname = new Label ("psurname", new PropertyModel<String>(user, "surname"));

				
				add(name);
				add(surname);
//---------------------------------------------date
				
				//Date field
				//DateTextField dateTextField = new DateTextField("dateTextField", new PropertyModel<Date>(
				  //      this, "date"), new StyleDateConverter("S-", true));
				
//				PropertyModel<Date> fecha = new PropertyModel<Date>(this,"fecha");
//				
//				DateTextField dateTextField = new DateTextField("tdate",fecha);
//				DatePicker datePicker = new DatePicker();
//				datePicker.setShowOnFieldClick(true);
//				datePicker.setAutoHide(true);
//				dateTextField.add(datePicker);
				
				 
				//Date and time fields
				DateTimeField dateTimeField = new DateTimeField("ttime", new PropertyModel<Date>(this, "fecha")){
				    /**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
				    protected boolean use12HourFormat() {
				        //this will force to use 24 hours format
				        return false;
				    }
				};
				
				
				
		

				
	//-----------------------------------------------------------------------
		add(Sform);
	    Sform.add(listOrigin);
	    Sform.add(listDestination);
	    Sform.add(ButtonsearchMeet);
		add(FormNewMeet);
		FormNewMeet.add(listVehicle);

		FormNewMeet.add(torigin);
		FormNewMeet.add(tdestination);
		FormNewMeet.add(tslots);
		FormNewMeet.add(ButtonsaveMeet);
		FormNewMeet.add(dateTimeField); 
		
		//Omplir el select
		tslots.add(new SelectOption<String>("s0", new Model<String>(
				"99")));
		tslots.add(new SelectOption<String>("s1", new Model<String>(
				"1")));
		tslots.add(new SelectOption<String>("s2", new Model<String>(
				"2")));
		tslots.add(new SelectOption<String>("s3", new Model<String>(
				"3")));
		tslots.add(new SelectOption<String>("s4", new Model<String>(
				"4")));
		Link p0 = new Link("p0"){
			public void onClick(){
				setResponsePage(Profile.class);
			}
		};
        Link p1 = new Link("p1"){
			public void onClick(){
				setResponsePage(Profile.class);
			}
		};
//        Link p2 = new Link("p2"){
//			public void onClick(){
//				setResponsePage(Profile.class);
//			}
//		};
        Link p3 = new Link("p3"){
			public void onClick(){
				setResponsePage(Profile.class);
			}
		};
		add(p0);
		add(p1);
//		add(p2);
		add(p3);
	}
}
